package cn.com.dycc.study_manage.integral.service;


import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.com.dycc.common.entity.Pb_Integral_LogCount;
import cn.com.dycc.common.entity.Pb_Integral_LogCountOrder;
import cn.com.dycc.party_org.organize.dao.OrganizeDao;
import cn.com.dycc.party_org.organize.entity.OrganizeEntity;
import cn.com.dycc.study_manage.integral.dao.Pb_Integral_LogDao;
import cn.com.dycc.study_manage.integral.entity.Pb_Integral_Log;
import cn.com.dycc.study_manage.integral.param.Pb_Integral_LogParam;
import cn.com.dycc.util.ConfigRegex;
import cn.com.dycc.util.DateUtil;
import cn.com.dycc.util.JsonObjectResult;
import cn.com.dycc.util.PageObjectUtil;
import cn.com.dycc.util.StringUtil;

@Service("pb_Integral_LogService")
@Transactional
public class Pb_Integral_LogServiceImpl implements Pb_Integral_LogService{

	@Resource
	private Pb_Integral_LogDao pb_Integral_LogDao;
	
	@Resource
	private OrganizeDao organizeDao;
	
	
	

	

	

	public JsonObjectResult findPb_Integral_LogByParam(Pb_Integral_LogParam param,
			HttpServletRequest request) throws Pb_Integral_LogNotFoundException {
		List<Pb_Integral_Log> lists = new ArrayList<Pb_Integral_Log>();
		
		checkParam(param);
		param = realParam(param, request);
		String count = pb_Integral_LogDao.getPb_Integral_LogCountByParam(param);
		
		if ("0".equals(count)) {			
			return new JsonObjectResult(lists);
		}
		param = (Pb_Integral_LogParam) PageObjectUtil.getSqlPageObject(count, param);
		lists = pb_Integral_LogDao.findPb_Integral_LogByParam(param);
		
		
		return new JsonObjectResult(count,lists);
	}
	
	/**
	 * 实际参数
	 * 
	 * @param param
	 * @param request
	 * @return
	 */
	public Pb_Integral_LogParam realParam(Pb_Integral_LogParam param, HttpServletRequest request) {
		if(param.getLog_des()!=null&&!param.getLog_des().trim().isEmpty()){
			param.setLog_des(StringUtil.format_mh_str(param.getLog_des()));
		}
		if(param.getUser_name()!=null&&!param.getUser_name().trim().isEmpty()){
			param.setUser_name(StringUtil.format_mh_str(param.getUser_name()));
		}
		if(param.getOrder()==null||param.getOrder().isEmpty()||"1".equals(param.getOrder())){
			param.setOrder("p.create_date desc");
		}else if("2".equals(param.getOrder())){
			param.setOrder("p.create_date asc");
			
		}else{
			param.setOrder(null);//手动防止SQL注入
		}
		return param;
	}
	
	/**
	 * 校验 @param param @return @throws
	 */
	public void checkParam(Pb_Integral_LogParam param) {
		if (param == null) {
			throw new Pb_Integral_LogNotFoundException("未登录或超时,请重新登录");
		}
		long start=0;
		long end=0;
		StringBuilder builder=new StringBuilder();
		if(param.getCreate_date()!=null&&!param.getCreate_date().trim().isEmpty()){
			if(!param.getCreate_date().matches(ConfigRegex.DATEREGEX)){
				builder.append("开始时间格式不正确,应为YYYY-MM-DD;");
			}else{
				start=DateUtil.format_StringToDate(param.getCreate_date()).getTime();
			}
			
		}
		if(param.getCreate_date_e()!=null&&!param.getCreate_date_e().trim().isEmpty()){
			if(!param.getCreate_date_e().matches(ConfigRegex.DATEREGEX)){
				builder.append("开始时间格式不正确,应为YYYY-MM-DD;");
			}else {
				end=DateUtil.format_StringToDate(param.getCreate_date_e()).getTime();
				if(start-end>0){
					builder.append("开始时间大于结束时间;");
				}
			}
			
		}
		
		if(!"".equals(builder.toString())){
			throw new Pb_Integral_LogNotFoundException(builder.toString());
		}

	}

	/*public List<Pb_Integral_Log> getServiceList(Pb_Integral_LogParam param) {
		Pb_Integral_Log entity = new Pb_Integral_Log();
		
		entity.setLog_id(param.getLog_id());
		entity.setIntegral_val(StringUtil.format_StringToInteger(param.getIntegral_val()));
		entity.setLog_flag(param.getLog_flag());
		entity.setLog_des(param.getLog_des());
		entity.setUser_id(param.getUser_id());
		entity.setDept_id(param.getDept_id());
		
		entity.setState(param.getState());
		entity.setOrg_id(param.getOrg_id());
		if(param.getCreate_date()!=null&&!param.getCreate_date().trim().isEmpty()){
			entity.setCreate_date(Timestamp.valueOf(param.getCreate_date()));
		}
		
		
		List<Pb_Integral_Log> lists = new ArrayList<Pb_Integral_Log>();
		lists.add(entity);
		return lists;
	}*/

	public JsonObjectResult findPb_Integral_LogCountByParam(Pb_Integral_LogParam param, HttpServletRequest request)
			throws Pb_Integral_LogNotFoundException {
		
		List<Pb_Integral_LogCount> lists = new ArrayList<Pb_Integral_LogCount>();
		
		checkParam(param);
		param = realParam(param, request);
		String count = pb_Integral_LogDao.getStatisticsCountByParam(param);
		
		if ("0".equals(count)) {			
			return new JsonObjectResult(lists);
		}
		param = (Pb_Integral_LogParam) PageObjectUtil.getSqlPageObject(count, param);
		lists = pb_Integral_LogDao.findPb_Integral_LogCountByParam(param);
		
		
		return new JsonObjectResult(count,lists);
	}
	
	
	public JsonObjectResult findPb_Integral_LogCountAppByParam(Pb_Integral_LogParam param, HttpServletRequest request)
			throws Pb_Integral_LogNotFoundException {
			
		checkParam(param);
		HttpSession session=request.getSession();
		String user_id=(String)session.getAttribute("id");
		String dept_id=(String)session.getAttribute("dept_id");
		String username=(String)session.getAttribute("username");
		String org_id=(String)session.getAttribute("org_id");
		param.setOrg_id(org_id);
		Pb_Integral_LogCount pb_Integral_LogCount=new Pb_Integral_LogCount();//当登录用户为积分信息时,为其添加空记录给前端
		if(dept_id==null||dept_id.trim().isEmpty()){
			throw new Pb_Integral_LogNotFoundException("该用户不是党员");
		}
		OrganizeEntity org=organizeDao.findOrganizeByDeptId(dept_id,org_id);
		if(org==null){
			throw new Pb_Integral_LogNotFoundException("该党员的党组织不存在");
		}
		String dept_name=org.getDept_name();
		
		List<Pb_Integral_LogCount> lists = new ArrayList<Pb_Integral_LogCount>();
		param = realParam(param, request);
		String count = pb_Integral_LogDao.getStatisticsCountByParam(param);
		
		if ("0".equals(count)) {			
			return new JsonObjectResult(lists);
		}
		param = (Pb_Integral_LogParam) PageObjectUtil.getSqlPageObject(count, param);
		lists = pb_Integral_LogDao.findPb_Integral_LogCountByParam(param);
		boolean flag=true;
		for(Pb_Integral_LogCount entity:lists){
			if(user_id.equals(entity.getUser_id())&&dept_id.equals(entity.getDept_id())){
				System.out.println("当前党员存在积分信息，党员id为:"+user_id);
				flag=false;
				break;
			}
		}
		System.out.println("统计总条数0:"+lists.size());
		if (flag) {
			pb_Integral_LogCount.setCount("0");
			pb_Integral_LogCount.setDept_id(dept_id);
			pb_Integral_LogCount.setDept_name(dept_name);
			pb_Integral_LogCount.setUser_id(user_id);
			pb_Integral_LogCount.setUser_name(username);
			lists.add(lists.size(), pb_Integral_LogCount);
			
		}
		System.out.println("统计总条数1:"+lists.size());
		
		return new JsonObjectResult(count,lists);
	}

	public JsonObjectResult findPb_Integral_LogCountOrderByParam(Pb_Integral_LogParam param, HttpServletRequest request)
			throws Pb_Integral_LogNotFoundException {
		Pb_Integral_LogCountOrder order=new Pb_Integral_LogCountOrder();
		HttpSession session=request.getSession();
		String dept_id=(String)session.getAttribute("dept_id");
		String user_id=(String)session.getAttribute("id");
		String username=(String)session.getAttribute("username");
		String org_id=(String)session.getAttribute("org_id");
		param.setOrg_id(org_id);
		if(dept_id==null||dept_id.trim().isEmpty()){
			throw new Pb_Integral_LogNotFoundException("该用户不是党员");
		}
		OrganizeEntity org=organizeDao.findOrganizeByDeptId(dept_id,org_id);
		if(org==null){
			throw new Pb_Integral_LogNotFoundException("该党员的党组织不存在");
		}
		String dept_name=org.getDept_name();
		order.setUser_id(user_id);
		order.setUser_name(username);
		order.setDept_id(dept_id);
		order.setDept_name(dept_name);
		
		Pb_Integral_LogParam param2=new Pb_Integral_LogParam();
		String count = pb_Integral_LogDao.getStatisticsCountByParam(param2);
		if ("0".equals(count)) {
			order.setCount("0");
			order.setOrder(0);		
			return new JsonObjectResult("1",order);
		}
		param2 = (Pb_Integral_LogParam) PageObjectUtil.getSqlPageObject(count, param2);
		List<Pb_Integral_LogCount> lists=new ArrayList<Pb_Integral_LogCount>();
		lists = pb_Integral_LogDao.findPb_Integral_LogCountByParam(param2);
		boolean flag=true;//用于标识集合lists中是否存在登录用户的党员积分信息
		for (int i = 0; i < lists.size(); i++) {
			if(user_id.equals(lists.get(i).getUser_id())&&dept_id.equals(lists.get(i).getDept_id())){
				order.setCount(lists.get(i).getCount());
				order.setOrder(i+1);
				flag=false;
				break;
			}
		}
		if(flag){
			order.setCount("0");
			order.setOrder(lists.size()+1);
		}
		return new JsonObjectResult("1",order);
	}
	
}
